// ==UserScript==
// @name         Myscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @grant        GM_xmlhttpRequest
// @match        https://www.bevol.cn/composition/*
// @require      https://code.jquery.com/jquery-latest.js


// ==/UserScript==

(function() {
    'use strict';
    if(typeof jQuery == 'undefined'){
        alert("没有jquery");
    }
    console.log(typeof GM_xmlhttpRequest);
    unsafeWindow.foo= () => {
        console.log('hello world');
    };


    var bt1 = document.createElement("div");

    bt1.setAttribute("id", "kukukaki");
    //bt1.setAttribute("style", "z-index:9999;float:right;width:250px;border:1px solid #00F;height:650px");
    bt1.setAttribute("style", "top:100px;border:3px solid #407429;background:#f5f5f5;z-index:9999;width:300px;position:fixed;right:0px;bottom:134px;_position:absolute;_top:expression(documentElement.scrollTop+documentElement.clientHeight - this.clientHeight - 134+'px');display:block;");

    //  bt1.setAttribute("onclick", "window.location.reload()");

    var b = document.body;

    b.insertBefore(bt1, b.firstChild);
    var html = "<button id='kukukaki_Btn'   value='test' style='width:300px;height:40px'>保存</button>";
    html += "<textarea rows='1' style='border:1px solid #cfe2f3;width:295px' id='kukukaki_1'></textarea>";
    html += "<textarea rows='1' style='border:1px solid #cfe2f3;width:295px' id='kukukaki_2'></textarea>";
    html += "<textarea rows='1' style='border:1px solid #cfe2f3;width:295px' id='kukukaki_3'></textarea>";
    html += "<textarea rows='1' style='border:1px solid #cfe2f3;width:295px' id='kukukaki_4'></textarea>";
    html += "<textarea rows='1' style='border:1px solid #cfe2f3;width:295px' id='kukukaki_5'></textarea>";
    html += "<textarea rows='1' style='border:1px solid #cfe2f3;width:295px' id='kukukaki_6'></textarea>";
    html += "<textarea rows='10' style='border:1px solid #cfe2f3;width:295px;height:200px; overflow-y:auto ;' id='kukukaki_7'></textarea>";
    html += "<div style='border:1px solid #cfe2f3;width:295px' id='kukukaki_8'><input id='kukukaki_yfsy' name='Fruit' type='checkbox' value='' />孕妇慎用 </div>";
    html += "<div style='border:1px solid #cfe2f3;width:295px' id='kukukaki_9'></div>";
    $("#kukukaki").html(html);




    var $dd =  $(".component-info-title").children();

    var len = $dd.length;
    var tmpstr = "";

    for(var  i=0;i<len;i++){
        tmpstr = $dd[i].innerHTML;
       // tmpstr =  tmpstr.substring(tmpstr.indexOf("：")+1).replace(/[\r\n]/g,"").replace(/(^\s*)|(\s*$)/g, "").replace(/&nbsp;/g, "").replace(/	/g, " ");
         tmpstr =  tmpstr.substring(tmpstr.indexOf("：")+1).replace(/\s+/g,"").replace(/&nbsp;/g, " ").replace(/(^\s*)|(\s*$)/g, "");
 console.log("logging info: " +  tmpstr);
        $("#kukukaki_"+(i+1)).val(tmpstr);
    }

    // Your code here...$("#test").children()

    var $dc =  $(".component-info-box");
    var $dc1=$($dc[0]).children();
    tmpstr = $dc1[0].innerHTML;
   console.log("logging info: " +  tmpstr);
    tmpstr = tmpstr.replace(/美丽修行/g,"");
    $("#kukukaki_7").val(tmpstr);

    if(tmpstr.indexOf("孕妇慎用")>1 ||  tmpstr.indexOf("哺乳期")>1||  tmpstr.indexOf("妊娠期")>1 ||  tmpstr.indexOf("胎儿畸形")>1 ){
        $("#kukukaki_yfsy").attr("checked", true);
    }


    $("#kukukaki_Btn").bind("click",function(){
        var k1 =$("#kukukaki_1").val();
        var k2 =$("#kukukaki_2").val();
        var k3 =$("#kukukaki_3").val();
        var k4 =$("#kukukaki_4").val().replace(/;/g," ")
        var k5 =$("#kukukaki_5").val().replace(/[\r\n]/g,"").replace(/[\r\n]/g,"").replace(/(^\s*)|(\s*$)/g, "").replace(/&nbsp;/g, "");
        var k6 =$("#kukukaki_6").val().replace(/[\r\n]/g,"").replace(/[\r\n]/g,"").replace(/(^\s*)|(\s*$)/g, "").replace(/&nbsp;/g, "");
        var k7 =$("#kukukaki_7").val().replace(/美丽修行/g,"");
        var k8 = '0';
//k7='';

        if ($('#kukukaki_yfsy').attr('checked')) {
            // do something
            k8 = '1';
        }
        var datastr = '{"name":"'+encodeURI(k1)+'","inci":"'+encodeURI(k2)+'","alias":"'+encodeURI(k3)+'","cas":"'+encodeURI(k4)+'","use":"'+encodeURI(k5)+'","ewg":"'+k6+'","overview":"'+encodeURI(k7)+'","caution":"'+k8+'"}';


        GM_xmlhttpRequest({
            method: "POST",
            url: "http://127.0.0.1:8080/ingredient/addii",
            data: "key=spark&data="+datastr,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "charset":"UTF-8"
            },
            onload: function(response) {
                console.log("logging info: "+response.responseText);
                var rj =  JSON.parse(response.responseText);
                if (rj.r == 1) {
                    $("#kukukaki_9").html("<span style='color:#DC143C'>保存成功</span>");
                }else{
                    $("#kukukaki_9").html(rj.err);
                }
            }
        });
    });

    /*


*/


})();